<?php

/**
 * Page de validation d'un compte, recoit en parametère GET l'id de l'utilisateur et son key_unique
 * @version 20071005
 * @author Antoine Marcadet
 */

require('config.php');

$page = new rootLayoutPage();
$page->setBaliseTitle('validation_balise');
$page->setDescription('validation_description');

$xml = tmxTranslate('validation.xml', REP_XLAT_TEMPLATE);
$xml_subject = tmxTranslate('subject.mail.xml', REP_XLAT_MAIL);
//-- menu --------------------------------------------------------------------//
$menu = new layoutMenuLeft();
$menuPage = new layoutMenuPage();
$menu->pushMenu($menuPage);
$page->setMenuPage($menu);

//-- corps de la page --------------------------------------------------------//
$tpl_file='validation.tpl';
$validation = new flyLayout(REP_TPL . $tpl_file);
$validation->start();

try {
    if(!isset($_GET['id']) or !isset($_GET['clef']))
        throw new Exception($xml['error_message_1']);

    $user = modelUser::getUserIdKeyUnique($_GET['id'], $_GET['clef']);
    if(!$user)
        throw new Exception($xml['error_message_2']);

    if(isset($_SESSION['id_user'])) {
        //session_unset();
    	//session_destroy();
    	//session_start();
    	unset($_SESSION['id_user']);
    	unset($_SESSION['login']);
    }

    if(isset($_POST['password']) and isset($_POST['confirm'])) { // modification du mot de passe
        $password = addslashes($_POST['password']);
        $confirm  = addslashes($_POST['confirm']);
        if(($password != '' or $confirm != '') and $password == $confirm) {
            //modelUser::validation($user->id_user, $password);
            modelUser::validationByUser($user->id_user, $password);
            $key_unique   = time().rand(10000000, 99999999);
            modelUser::updateKeyUnique($user->id_user, $key_unique);

			$tpl_mail='confirmation.mail.tpl';
			$mail = new flyLayout(REP_MAIL . $tpl_mail);
        	$mail->start();

        	$mail->setVariable('email', $user->login);
        	$mail->stop();
        	$subject = $xml_subject['confirmation'].MAIL_SITENAME;

        	if(STATUS_EMAIL == 0) {
				echo $mail;
			}elseif(STATUS_EMAIL == 1) {
				monsterMail($user->login, $subject, $mail);
			}else {
				echo $mail;
				monsterMail($user->login, $subject, $mail);
			}

            modelUser::connexionMail($user->login);
            $validation->showBlock('confirmation');
            //Persistence::register(PERSIST_MESSAGE, new Message($xml['text_3_1'], Message::ERROR));
            //redirect('index');
        }
        else {
            Persistence::register(PERSIST_MESSAGE, new Message($xml['error_message_3'], Message::ERROR));
            $validation->showBlock('formulaire');
        }
    }
    else { // arrivée direct sur la page, pas de formulaire posté
        if($user->etat_user == modelUser::EMAIL_NON_VALIDE) {
    	//if($user->etat_user == modelUser::EMAIL_VALIDE) {
            $validation->showBlock('formulaire');
        }
        else { // si le compte est deja activé le lien ne doit plus marcher
            Persistence::register(PERSIST_MESSAGE, new Message($xml['error_message_4'], Message::AVERT));
        }
    }
}
catch(Exception $e) {
    Persistence::register(PERSIST_MESSAGE, new Message($e->getMessage(), Message::AVERT));
}

$validation->stop();
$page->setBody($validation);

// affichage
$page->display();

?>